const _ = wx.cloud.database().command
Page({

    /**
     * 页面的初始数据
     */
    data: {
        val: '',
        zone: '',
        city: '',
        province: '',
        isShow: false,
        isCity: false,
        isZone: false,
        listpro: [],
        listcity: [],
        listzone: [],
        listcon: [],
    },
    onShow: function () {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 1 //0,1，2 0-导航一  1-导航二  2-个人中心
            })
        }

    },
    onLoad: function (options) {
        // 这里的 name："home" 就是在 package.json中定义的名称
        wx.cloud.callFunction({
            name: "home",
            data: {
                type: 'province'
            },
        }).then(res => {
            console.log(res.result.data)
            this.setData({
                listpro: res.result.data
            })
        }).catch(err => {
            console.log(err)
        })
    },
    playopen: function () {
        if (this.data.isShow == false) {
            this.setData({
                isShow: true
            })
        }
        else {
            this.setData({
                isShow: false
            })
        }
        this.setData({
            val: ''
        })
    },
    cityopen: function () {
        if (this.data.isCity == false) {
            this.setData({
                isCity: true
            })
        }
        else {
            this.setData({
                isCity: false
            })
        }
    },
    proname: function (e) {
        let pro = e.currentTarget.dataset.name
        let id = e.currentTarget.dataset.id
        let that = this
        this.setData({
            province: pro
        })
        //渲染城市
        wx.cloud.database().collection('my_province').where({
            code: id,
            name: pro
        }).get({
            success(res) {
                // console.log("获取数据成功", res.data[0].children)
                that.setData({
                    listcity: res.data[0].children
                })
            },
            fail(res) {
                console.log("获取数据失败", res)
            }
        })
        this.playopen();
        this.Allsearch(pro);
    },
    cityname: function (e) {
        let name = e.currentTarget.dataset.name
        let id = e.currentTarget.dataset.id
        let that = this
        this.setData({
            city: name,
        })
        //渲染市区
        wx.cloud.database().collection('my_province').where({
            children: _.elemMatch({ code: _.eq(id) }),
        }).get({
            success(res) {
                console.log("获取数据成功", res.data[0].children)
                let arr = ''
                res.data[0].children.forEach(element => {
                    if (element.code == id) {
                        arr = element
                    }
                });
                console.log(arr);
                that.setData({
                    listzone: arr.children
                })
            },
            fail(res) {
                console.log("获取数据失败", res)
            }
        })
        this.cityopen();
        this.zoneopen();
        this.Allsearch(name);
    },
    zonename: function (e) {
        let name = e.currentTarget.dataset.name
        let id = e.currentTarget.dataset.id
        let that = this
        this.setData({
            zone: name,
        })
        //渲染列表
        this.Allsearch(name);
    },
    zoneopen: function () {
        this.setData({
            isZone: true
        })
    },
    //输入框模糊搜索
    searchplay: function () {
        let inputValue = this.data.val
        if (inputValue.length == 0) {
            return
        }
        wx.cloud.callFunction({
            name: 'home',
            data: {
                type: 'search',
                inputValue: inputValue
            }
        }).then(res => {
            console.log("输入框搜索",res)
            this.dataPlay(res.result.data, inputValue)
            // this.setData({
            //   resultArr: res.result.data
            // })
        })
        this.setData({
            isZone:false,
            listcity:[],
            city:'',
            province:''
        })
    },
    //省市区选择模糊搜索
    Allsearch: function (vaule) {
        let inputValue = vaule
        console.log(inputValue);
        if (inputValue.length == 0) {
            return
        }
        wx.cloud.callFunction({
            name: 'home',
            data: {
                type: 'search',
                inputValue: inputValue
            }
        }).then(res => {
            this.dataPlay(res.result.data, inputValue)
            console.log(res)
        })
    },
    //数据处理
    dataPlay: function (arr, value) {

        if (arr && arr.length > 0) {
            let list = this.data.listcon
            arr.forEach(item => {
                if (list.length > 0) {
                    item.issue.forEach(el => {
                        let count = 0;
                        let one = el.address.indexOf(value);
                        let two = el.addressname.indexOf(value);
                        let three = el.title.indexOf(value);
                        if (one >= 0 || two >= 0 || three >= 0) {
                            list.forEach(li => {
                                if (li.titid == el.titid) {
                                    count++;
                                }
                            })
                            if (count == 0) {
                                list = list.concat(el)
                            }
                        }
                    })
                } else {
                    item.issue.forEach(el => {
                        let one = el.address.indexOf(value);
                        let two = el.addressname.indexOf(value);
                        let three = el.title.indexOf(value);
                        if (one >= 0 || two >= 0 || three >= 0) {
                            list = list.concat(el)
                        }
                    })
                }

            })
            // this.setData({
            //     listcon: list
            // })
            this.complay(list, value)
            // console.log(this.data.listcon);
        }


    },
    //渲染内容处理
    complay: function (list, value) {
        let arr = list.filter(item => {
            let one = item.address.indexOf(value);
            let two = item.addressname.indexOf(value);
            let three = item.title.indexOf(value);
            if(one>=0 || two>=0 ||three >=0){
                return item
            }
        })
        this.setData({
            listcon:arr
        })
        console.log("结果",arr);
    },
     //跳转到具体文章
     urlnew:function(event){
        // console.log(event);
        let id = event.currentTarget.dataset.id
        console.log(id);
        wx.navigateTo({
            url: '/mypages/news/news?titid='+id,
            //   events: events,
            success: (result) => { },
            fail: (res) => { },
            complete: (res) => { },
        })
    },


})